import { ApiaddComment  } from "../../../api/index"
import { debounce } from "../../../utils/index"
const tagsList = ["回复很及时","解答很有帮助","非常耐心","准确用药"]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tagsList,
    selectTag: {},
    content: "",
    score: 0,
    id: null,
    btnLoading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.id = options.id
  },

  onSubmit: debounce(function() {
    if(!this.data.content) {
      wx.showToast({
        title: '请输入评价内容！',
        icon: "none"
      })
      return
    }
    if(!this.data.score){
      wx.showToast({
        title: '请选择总体评分',
        icon: "none"
      })
      return
    }

    this.setData({
      btnLoading: true
    })
    const ops = {
      content: this.data.content,
      score: this.data.score,
      tags: Object.values(this.data.selectTag)
    }
    ApiaddComment(ops,this.data.id).then(res => {
      wx.showModal({ 
        title: '提示',
        content: '评价提交成功！',
        showCancel: false,
        confirmText: "返回",
        success:(res) => {
          if (res.confirm) {
            wx.navigateBack()
          } 
        }
      })
        
      
    }).finally(_=>{
      this.setData({
        btnLoading: false
      })
    })
  },100,false),

  onInput(e) {
    this.setData({
      content: e.detail.value
    })
  },

  onChange(e) {
    if(e.detail !== this.data.score) {
      this.setData({
        score: e.detail
      })
    }
  },

  onClickTag(e) {
    const index = e.currentTarget.dataset.index
    const selectTag = this.data.selectTag
    if(selectTag[index]){
      delete selectTag[index]
    }else {
      selectTag[index] = tagsList[index]
    }
    this.setData({
      selectTag
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})